package isv

import (
	"dhfshop/controllers/base"
	"dhfshop/models/isv"
	"time"
	"dhfshop/models/consts"
	"github.com/astaxie/beego/validation"
)

type UserAppController struct {
	base.BaseController
}

func (this *UserAppController) Get() {
	query, _,fields, order_by, offset, limit, err := this.GetQueryPara()
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	l, cnt, err := isv.GetAllUserApps(query, fields, order_by, offset, limit)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		TotalCount int64        `json:"total_count"`
		UserApps     []*isv.UserApp `json:"user_apps"`
	}{TotalCount: cnt, UserApps: l}
	this.Data["json"] = r
	this.ServeJSON()
}

func (this *UserAppController) Post() {
	var v isv.UserApp
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	if len(v.UploadTimeStr) == 19 {
	    v.UploadTime, _ = time.Parse(consts.DATETIME_FORMAT,v.UploadTimeStr)
	}
	if len(v.SubaduitTimeStr) == 19 {
	    v.SubaduitTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SubaduitTimeStr)
	}
	if len(v.PublishTimeStr) == 19 {
	    v.PublishTime, _ = time.Parse(consts.DATETIME_FORMAT,v.PublishTimeStr)
	}
	if len(v.VisibleTimeStr) == 19 {
	    v.VisibleTime, _ = time.Parse(consts.DATETIME_FORMAT,v.VisibleTimeStr)
	}
	id, err := isv.AddUserApp(&v)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	v.UserAppId = id
	this.Data["json"] = v
	this.ServeJSON()
}

func (this *UserAppController) Put() {
	var v isv.UserApp
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.UserAppId, "user_app_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	if len(v.UploadTimeStr) == 19 {
	    v.UploadTime, _ = time.Parse(consts.DATETIME_FORMAT,v.UploadTimeStr)
	}
	if len(v.SubaduitTimeStr) == 19 {
	    v.SubaduitTime, _ = time.Parse(consts.DATETIME_FORMAT,v.SubaduitTimeStr)
	}
	if len(v.PublishTimeStr) == 19 {
	    v.PublishTime, _ = time.Parse(consts.DATETIME_FORMAT,v.PublishTimeStr)
	}
	if len(v.VisibleTimeStr) == 19 {
	    v.VisibleTime, _ = time.Parse(consts.DATETIME_FORMAT,v.VisibleTimeStr)
	}
	updateFields, err := this.GetUpdateFields(v,"", "form", []string{""})
	if err != nil {
	  this.ServeError(err.Error())
		return
	}
	if len(updateFields) > 0 {
	    _, err := isv.UpdateUserAppById(&v, updateFields...)
		if err != nil {
			this.ServeError(err.Error())
			return
		}
	}
	rsp := struct {
		UserAppId int64       `json:"user_app_id"`
	    Modified  time.Time   `json:"modified"`
	}{UserAppId:v.UserAppId, Modified:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *UserAppController) Delete() {
	var v isv.UserApp
	this.ParseForm(&v)
	valid := validation.Validation{}
	valid.Required(v.UserAppId, "user_app_id")
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key + " " + valid.Errors[0].Message)
		return
	}
	err := isv.DeleteUserAppById(v.UserAppId)
	if err != nil {
		this.ServeError(err.Error())
		return
	}
	r := struct {
		UserAppId int64     `json:"user_app_id"`
		Modified  time.Time `json:"modified"`
	}{UserAppId: v.UserAppId, Modified: time.Now().UTC()}
	this.Data["json"] = r
	this.ServeJSON()
}
